<template>
      <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="表名称">
                <a-input v-model="queryParam.tableName" placeholder="请输入" allow-clear/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="表描述">
                <a-input v-model="queryParam.tableComment" placeholder="请输入" allow-clear/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="创建时间">
                  <a-range-picker style="width: 100%" v-model="dateRange" valueFormat="YYYY-MM-DD" format="YYYY-MM-DD" allow-clear/>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-list
        rowKey="tableName"
        :grid="{gutter: 24, lg: 3, md: 2, sm: 1, xs: 1}"
        :dataSource="list"
        class="card-list"
      >
        <a-list-item slot="renderItem" slot-scope="item">
          <template v-if="!item || item.tableName === undefined">
            <a-button class="new-btn" type="dashed" @click="$refs.createForm.handleAdd()">
              <a-icon type="plus"/>
              新增表
            </a-button>
          </template>
          <template v-else>
            <a-card :hoverable="true">
              <a-card-meta>
                <a slot="title">{{ item.tableComment }}</a>
                <a-avatar class="card-avatar" slot="avatar" :src="tableIcon" size="large"/>
                <div class="meta-content" slot="description">
                  <a-icon type="database" :style="{color: '#1890ff'}" />
                  {{ item.tableName }}
                  <div>{{ item.remark }}</div>
                </div>
              </a-card-meta>
              <template class="ant-card-actions" slot="actions">
                <a @click="handleEditTable(item)" v-hasPermi="['tool:gen:edit']">编辑</a>
                <a @click="handleDelete(item)" v-hasPermi="['tool:gen:remove']">删除</a>
              </template>
            </a-card>
          </template>
        </a-list-item>
      </a-list>

      <!-- 分页 -->
      <a-pagination
        class="ant-table-pagination"
        show-size-changer
        show-quick-jumper
        :current="queryParam.pageNum"
        :total="total"
        :page-size="queryParam.pageSize"
        :showTotal="total => `共 ${total} 条`"
        @showSizeChange="onShowSizeChange"
        @change="changeSize"
      />

      <!-- 增加修改 -->
      <create-form
        ref="createForm"
        @ok="getList"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import tableIcon from '@/assets/images/table-icon.png'
import { delTable, genCode } from '@/api/tool/gen'
export default {
  name: 'CardLayout',
  components: {
  },
  data () {
    return {
      list: [],
      tableIcon: tableIcon,
      selectedRowKeys: [],
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中表数组
      tableNames: [],
      loading: false,
      total: 0,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        tableName: undefined,
        tableComment: undefined
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询表集合 */
    getList () {
      this.loading = true
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        tableName: undefined,
        tableComment: undefined
      }
      this.handleQuery()
    },
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.tableNames = this.selectedRows.map(item => item.tableName)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const tableNames = row.tableName || this.tableNames.join(',')
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '为保证数据安全，此操作仅删除代码生成的配置信息，数据库表请自行删除！',
        onOk: () => {
          return delTable(tableNames)
            .then(() => {
              this.onSelectChange([], [])
              this.getList()
              this.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 修改按钮操作 */
    handleEditTable (row) {
      const tableName = row.tableName || this.tableNames[0]
      this.$router.push({
        name: 'GenEdit',
        params:
        {
          tableName: tableName
        }
      })
    },
    /** 生成代码操作 */
    handleGenTable (row) {
      const tableNames = row.tableName || this.tableNames
      if (tableNames === '') {
        this.$message.error(
          '请选择要生成的数据',
          3
        )
        return
      }
      if (row.genType === '1') {
        genCode(row.tableName).then(response => {
          this.msgSuccess('成功生成到自定义路径：' + row.genPath)
        })
      } else {
        // downLoadZip('/tool/gen/batchGenCode?tables=' + tableNames, 'ruoyi')
      }
    },
    handleOk () {
      this.resetQuery()
    }
  }
}
</script>

<style lang="less" scoped>
@import "~@/components/index.less";
.card-list {
  /deep/ .ant-card-body:hover {
    .ant-card-meta-title>a {
      color: @primary-color;
    }
  }
  /deep/ .ant-card-meta-title {
    margin-bottom: 12px;
    &>a {
      display: inline-block;
      max-width: 100%;
      color: rgba(0,0,0,.85);
    }
  }
  /deep/ .meta-content {
    position: relative;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    height: 40px;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    margin-bottom: 10px;
  }
}
.new-btn {
  background-color: #fff;
  border-radius: 2px;
  width: 100%;
  height: 175px;
}
.table-author {
  color: crimson;
}
</style>
